//#version 130

const float COLS  = 16.0;
const float ROWS  = 16.0;
const float STACK = 128.0;
const float CLUSTER_COLS = 5.0;
const float CLUSTER_ROWS = 5.0;
const float CYLINDER_SLICES = 128.0;
const float ROW_HEIGHT = 3.0;
const float TALL = 3.0;
const float WIDTH = 2.0*sqrt(3.0);
const float HALF_WIDTH = WIDTH / 2.0;
const float HEX_RADIUS = WIDTH * 2.0;

varying vec3 diffuseColor;

uniform sampler2D position;
uniform sampler3D hexData;
uniform sampler3D lightingA;
uniform sampler3D lightingB;

uniform int currCluster_i;
uniform int currCluster_j;

//const float ScaleFactor = 0.2;
const float C1 = 0.429043;
const float C2 = 0.511664;
const float C3 = 0.743125;
const float C4 = 0.886227;
const float C5 = 0.247708;

const vec3 L00  = vec3( 0.871297,   0.875222,	 0.864470);
const vec3 L1m1 = vec3( 0.175058,   0.245335,	 0.312891);
const vec3 L10  = vec3( 0.034675,   0.036107,	 0.037362);
const vec3 L11  = vec3(-0.004629,  -0.029448,	-0.048028);
const vec3 L2m2 = vec3(-0.120535,  -0.121160,	-0.117507);
const vec3 L2m1 = vec3( 0.003242,   0.003624,	 0.007511);
const vec3 L20  = vec3(-0.028667,  -0.024926,	-0.020998);
const vec3 L21  = vec3(-0.077539,  -0.086245,	-0.091591);
const vec3 L22  = vec3(-0.161784,  -0.191783,	-0.219152);

void main(void)
{
    vec3 posCoord;
    vec3 hexTypeCoord;
    vec3 lightingCoord;

    vec4 adj = vec4(0.0,0.0,0.0,1.0);
    vec4 currHexData;
    vec4 lightsA;
    vec4 lightsB;
    float cellHeight;

    gl_Position = vec4(0.0,0.0,0.0,0.0);//gl_ModelViewProjectionMatrix * (gl_Vertex);
    diffuseColor = vec3(0.0,0.0,0.0);


    float i = floor(mod(float(gl_InstanceID),COLS));
    float j = floor(mod(float(gl_InstanceID)/COLS,ROWS));
    float k = floor(mod(float(gl_InstanceID)/(COLS*ROWS),STACK));

//    hexTypeCoord.x = i / (CLUSTER_COLS * COLS);
//    hexTypeCoord.y = j / (CLUSTER_ROWS * ROWS);
//    hexTypeCoord.z = k / STACK;

    hexTypeCoord.x = (COLS * float(currCluster_i) + i) / (CLUSTER_COLS * COLS);
    hexTypeCoord.y = (ROWS * float(currCluster_j) + j) / (CLUSTER_ROWS * ROWS);
    hexTypeCoord.z = k / STACK;

    lightingCoord.x = i / (COLS);
    lightingCoord.y = j / (ROWS);
    lightingCoord.z = k / STACK;

    currHexData = texture(hexData,hexTypeCoord);
    float hexType = currHexData.r;
    float neighbors = currHexData.g;

    if(hexType > 0.0 && neighbors != 1.0){
	vec3 normal = (gl_Normal + 1.0)*0.25 + 0.75;


	adj.x = i * ROW_HEIGHT;
	adj.y = k * TALL;
	if(mod(abs(i),2.0)== 1.0){
	    adj.z = (j*WIDTH) - HALF_WIDTH;
	}else{
	    adj.z = (j*WIDTH);
	}

	float amount = currHexData.b;
//
//	cellHeight = adj.y;
//	float radiusNear = -400.0;
//	float radiusFar = -450.0;
//	float radiusDelta = radiusNear - radiusFar;

//	vec4 cylinder = gl_Vertex + adj;

//	float angle = radians(cylinder.z * 360.0/(ROWS*CYLINDER_SLICES*HEX_RADIUS) + 60);
//	float R = (cylinder.y/(STACK*cellHeight)) * (radiusDelta) + radiusFar;

//	cylinder.z = R * cos(angle);
//	cylinder.y = R * sin(angle);
//	cylinder.x *= 0.35;

//        gl_Position = gl_ModelViewProjectionMatrix * cylinder;
//
	gl_Position = gl_ModelViewProjectionMatrix * (gl_Vertex + adj);

	if(hexType >= 4.0){			    //unbreakable
	    diffuseColor.rgb = vec3(0.1,0.1,0.1);
	}else if(hexType >= 3.0 && hexType < 4.0){  //stone
	    diffuseColor.rgb = vec3(0.3,0.3,0.35);
	}else if(hexType >= 2.0 && hexType < 3.0){  //dirt
	    diffuseColor.rgb = vec3(0.4,0.3,0.0);
	}else if(hexType >= 1.0 && hexType < 2.0){  //sand
	    diffuseColor.rgb = vec3(1.0,0.8,0.2);
	}else if(hexType >= 0.0 && hexType < 1.0){  //empty
	    diffuseColor.rgb = vec3(0.0,0.0,0.0);
	}

	lightsA = texture3D(lightingA,lightingCoord);
	lightsB = texture3D(lightingB,lightingCoord);

	//spherical harmonics
	float ScaleFactor = 0.4*lightsA.r;
	vec3 tnorm = normalize(gl_NormalMatrix * gl_Normal);

	diffuseColor = C1 * L22 * (tnorm.x * tnorm.x - tnorm.y * tnorm.y) +
		       C3 * L20 * tnorm.z * tnorm.z +
		       C4 * L00 -
		       C5 * L20 +
		       2.0 * C1 * L2m2 * tnorm.x + tnorm.y +
		       2.0 * C1 * L21  * tnorm.x + tnorm.z +
		       2.0 * C1 * L2m1 * tnorm.y + tnorm.z +
		       2.0 * C2 * L11  * tnorm.x +
		       2.0 * C2 * L1m1 * tnorm.y +
		       2.0 * C2 * L10  * tnorm.z;

       diffuseColor *= ScaleFactor;

    }
 //   }
}